import 'package:flutter/material.dart';
import 'package:flutter_2b_client/components/showConfirmDialog.dart';
import 'package:flutter_2b_client/database/AppDatabase.dart';
import 'package:flutter_2b_client/database/dao/GoodsScratchItemDao.dart';
import 'package:flutter_2b_client/database/entity/GoodsScratch.dart';
import 'package:flutter_2b_client/utils/Page.dart';
import 'package:flutter_2b_client/utils/PagingUtils.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';

import '../components/MyPagedListView.dart';

class GoodsScratchWithItemAmount {
  final GoodsScratch goodsScratch;
  final int itemAmount;

  GoodsScratchWithItemAmount(this.goodsScratch, this.itemAmount);
}

class GoodsScratchPage extends HookWidget {
  static String path = "/goodsScratches";

  const GoodsScratchPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var pagingController =
        usePagingController<Pageable, GoodsScratchWithItemAmount>(
            firstPageKey: Pageable());
    useSlicingControllerListener(pagingController, (pageable) async {
      var appDatabase = (await AppDatabase.instance);
      var goodsScratchDao = appDatabase.goodsScratchDao;
      var goodsScratchItemDao = appDatabase.goodsScratchItemDao;
      return await Slice.fromOneMoreList(
        await goodsScratchDao.findAllByPageable(
            pageable.offset, pageable.limit + 1),
        pageable,
      ).mapAsync((e) async => GoodsScratchWithItemAmount(
          e, (await goodsScratchItemDao.countAllByGoodsScratchId(e.id!)) ?? 0));
    }, null, []);

    return Scaffold(
      appBar: AppBar(
        title: const Text("商品草稿单"),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        onPressed: () async {
          var instance = await AppDatabase.instance;
          var goodsScratch = GoodsScratch.create();
          var id =
              await instance.goodsScratchDao.insertGoodsScratch(goodsScratch);
          var list = pagingController.itemList ?? [];
          list.insert(
              0,
              GoodsScratchWithItemAmount(
                  goodsScratch..id=id,
                  await instance.goodsScratchItemDao
                          .countAllByGoodsScratchId(id) ??
                      0));
          if (list.length >= pagingController.firstPageKey.size) {
            list.removeLast();
          }
          pagingController.itemList = list.toList();
        },
      ),
      body: MyPagedListView<Pageable, GoodsScratchWithItemAmount>(
        pagingController: pagingController,
        itemBuilder:
            (BuildContext context, GoodsScratchWithItemAmount item, int index) {
          return GestureDetector(
            onLongPress: () {
              showConfirmDialog(
                context,
                onConfirm: ()async {
                  var instance = await AppDatabase.instance;
                  await instance.goodsScratchDao.deleteGoodsScratch(item.goodsScratch);

                  pagingController.itemList = pagingController.itemList!.toList()
                    ..remove(item);
                  if(context.mounted) {
                    Navigator.of(context).pop();
                  }
                },
                title: "确定要删除此草稿？",
                confirmText: "删除",
                content: null,
              );
            },
            child: ListTile(
              title: Text(item.goodsScratch.createdDateTime.format()),
              onTap: () {
                context
                    .push("/goodsScratches/${item.goodsScratch.id.toString()}");
              },
              trailing: Text("${item.itemAmount}个单品"),
            ),
          );
        },
      ),
    );
  }
}
